﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Sql;

public partial class login : System.Web.UI.Page
{
    private DcSql conn;
    private string VT_Prim = "Prim";

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
           
            if (Page.IsPostBack == false)
            {
                Session.Add("IsAuthenticated", "0");
            }
        }
        catch (Exception oExp)
        {
            // Exception:
            lblLoginFailed.Text = oExp.ToString();
        }

        if (ConfigurationManager.AppSettings["PassLogin"].ToString() == "true")
            LoginUser(ConfigurationManager.AppSettings["PassLoginUser"].ToString(), ConfigurationManager.AppSettings["PassLoginPassword"].ToString());
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        LoginUser(txtUserName.Text, txtLoginPass.Text);
    }

    private void LoginUser(string UserName, string Password)
    {
        // Authenticate user:
        try
        {
            conn = new DcSql();
            conn.DBBaglan(VT_Prim);

            string sql = string.Format(
                @"SELECT * FROM Kullanıcı WHERE sKullaniciAdi = '{0}' AND sSifre = '{1}'"
                ,UserName
                ,Password
            );

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(sql, conn.GetConn());

            da.Fill(ds, "Kullanici");

            // Get user info:
            // DataTable dtUserInfo = DB.OpenQuery(SQL);
            if (ds.Tables[0].Rows.Count == 1)
            {
                Session["IsAuthenticated"] = "1";
                Session["UserId"] = ds.Tables[0].Rows[0]["KullaniciID"].ToString();
                // Get userName:
                Session["UserName"] = this.txtUserName.Text;
                Session["SessionStart"] = DateTime.Now.ToString();

                SaveOnlineUser();

                // Redirect page:					
                Response.Redirect("Default.aspx", false);
            }
            else
            {
                this.lblLoginFailed.Text = "Hatalı kullanıcı adı veya şifre!";
            }

        }
        catch (Exception oExp)
        {
            lblGeneralException.Text = oExp.ToString();
        }

    }

    private void SaveOnlineUser()
    {
        try
        {
            // + "', '" + System.Net.Dns.GetHostByAddress(Request.UserHostName).HostName +
            string SQL = "INSERT INTO OnlineKullanicilar(KullanicID, OturumBaslangic, OturumBitis, IsOnline, IpAdres) VALUES( " + Session["UserId"].ToString() + ", ' ', ' ', 1 , '" + Request.UserHostName + "')";
            
            SqlCommand cmd = new SqlCommand(SQL, conn.GetConn());
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteScalar();
        }
        catch (Exception exc)
        {
            lblGeneralException.Text = exc.ToString();
        }
    }
}
